package com.jy.modules.cims.component.image;

import java.util.Observable;
import java.util.Observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.data.common.dao.TCMmQueueDao;
import com.jy.modules.cims.data.common.dto.TCMmQueueDTO;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.LoanDetailDto;

/**
 * @description 处理影像观察者
 * @author shengzhoutao
 * @date 2015-09-29
 */
@Component
public class TransImagesObserver implements Observer {
	private static Logger logger = LoggerFactory.getLogger(TransImagesObserver.class);

	@Autowired
	public TransferImagesDao transferImagesDao;

	@Autowired
	private TCMmQueueDao queueDao;

	@Override
	public void update(Observable o, Object params) {
		try {
			if (params instanceof TransferImagesDto) {
				TransferImagesDto transferImagesDto = (TransferImagesDto) params;
				String contractNo = transferImagesDto.getContractNo();
				String dealType = transferImagesDto.getDealType();
				String accessoryFtpUrl = transferImagesDto.getAccessoryFtpUrl();

				// 修改债权明细的交互状态
				if (CimsConstant.DEAL_TYPE_CREDIT_OUT.equals(dealType) || CimsConstant.DEAL_TYPE_CREDIT_IN.equals(dealType)) {
					CreditDetailDto creditDetailDto = new CreditDetailDto();
					creditDetailDto.setCreditCode(contractNo);
					creditDetailDto.setTransferId(transferImagesDto.getTransferId());
					creditDetailDto.setAccessoryFtpUrl(accessoryFtpUrl);
					creditDetailDto.setCreditEachStatus(CimsConstant.LOAN_EACH_STATUS_SEND);
					transferImagesDao.updateCreditDetail(creditDetailDto);
				}
				// 修改贷款明细的交互状态
				else if (CimsConstant.DEAL_TYPE_LOAN_OUT.equals(dealType) || CimsConstant.DEAL_TYPE_LOAN_IN.equals(dealType)) {
					LoanDetailDto loanDetailDto = new LoanDetailDto();
					loanDetailDto.setContractNo(contractNo);
					loanDetailDto.setDealCode(transferImagesDto.getDealCode());
					loanDetailDto.setAccessoryFtpUrl(accessoryFtpUrl);
					loanDetailDto.setLoanEachStatus(CimsConstant.LOAN_EACH_STATUS_SEND);
					transferImagesDao.updateLoanDetail(loanDetailDto);
				}
				
				TCMmQueueDTO updateQueue = new TCMmQueueDTO();
				updateQueue.setQueueCode(contractNo);
				updateQueue.setQueueStatus(CimsConstant.EXE_STATUS_BEGIN);
				updateQueue.setNewQueueStatus(CimsConstant.EXE_STATUS_END);
				queueDao.updateTCMmQueue(SearchParamUtil.getSearchParam(updateQueue));
				
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}

}
